package lancs.mobilemedia.core.ui.controller;

import lancs.mobilemedia.core.ui.datamodel.ImageData;
import lancs.mobilemedia.core.ui.screens.PhotoListScreen;

public class SortableImageListerDecorator extends ImageListerDecorator {

	public SortableImageListerDecorator(ImageLister controller) {
		super(controller);
	}

	private void exchange(ImageData[] images, int pos1, int pos2) {
		ImageData tmp = (ImageData) images[pos1];
		images[pos1] = images[pos2];
		images[pos2] = tmp;
	}

	public void bubbleSort(ImageData[] images) {
		System.out.print("Sorting by BubbleSort...");
		for (int end = images.length; end > 1; end--) {
			for (int current = 0; current < end - 1; current++) {
				if (images[current].getNumberOfViews() > images[current + 1].getNumberOfViews()) {
					exchange(images, current, current + 1);
				}
			}
		}
		System.out.println("done.");
	}

	public void createImageList(PhotoListScreen imageList, ImageData image, boolean favorite) {
		super.createImageList(imageList, image, favorite);

	}

	public void sort(PhotoListScreen imageList, ImageData[] images, boolean sort) {
		if (sort) {
			bubbleSort(images);
		}
		super.sort(imageList, images, sort);
	}
}
